package com.weimob.mcloud.wechatwork.customer.convertor.customer;

import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.ExternalContactBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.FollowUserBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.onecrm.response.ExternalUserBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.passport.response.PassPortAccountMappingRes;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.CustomerEmployeeRelation;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.Customer;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Employee;
import com.weimob.mcloud.wechatwork.core.enums.customer.CustomerStatusEnum;
import com.weimob.mcloud.wechatwork.core.enums.customer.ExternalUserAddWayEnum;
import com.weimob.mcloud.wechatwork.core.enums.customer.ExternalUserTypeEnum;
import com.weimob.mcloud.wechatwork.core.enums.customer.GenderEnum;
import com.weimob.mcloud.wechatwork.core.enums.employee.EmployeeActiveStatusEnum;
import com.weimob.mcloud.wechatwork.core.enums.employee.EmployeeStatusEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.ExternalUserListBO;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/11 3:02 下午
 */
public class CustomerConvert {

    public static Customer toCustomer(PrivateSphere privateSphere, String externalUserId, String phone, ExternalContactBO externalContactBO) {
        Customer customer = new Customer();
        customer.setPrivateSphere(privateSphere);
        customer.setExternalUserid(externalUserId);
        customer.setName(externalContactBO.getName());
        customer.setAvatar(externalContactBO.getAvatar());
        customer.setGender(GenderEnum.getEnumByValue(externalContactBO.getGender()));
        customer.setPosition(externalContactBO.getPosition());
        customer.setCorpName(externalContactBO.getCorpName());
        customer.setCorpFullName(externalContactBO.getCorpFullName());
        customer.setExternalUserType(ExternalUserTypeEnum.getEnumByValue(externalContactBO.getType()));
        customer.setUnionid(externalContactBO.getUnionid());
        if (phone != null) {
            customer.setPhone(phone);
            customer.setMobiles(phone);
        }

        return customer;
    }

    public static CustomerEmployeeRelation toCustomerEmployeeRelation(String corpId, String customerId, Employee employee,
                                                                      Date followUserCreateTime, FollowUserBO followUserBO) {

        CustomerEmployeeRelation customerEmployeeRelation = new CustomerEmployeeRelation();
        customerEmployeeRelation.setCorpId(corpId);
        customerEmployeeRelation.setCustomerId(customerId);


        customerEmployeeRelation.setEmployee(employee);
        customerEmployeeRelation.setAddTime(followUserCreateTime);
        customerEmployeeRelation.setAddWay(ExternalUserAddWayEnum.getEnumBycode(followUserBO.getAddWay()));
        customerEmployeeRelation.setRemark(followUserBO.getRemark());
        customerEmployeeRelation.setRemarkCorpName(followUserBO.getRemarkCorpName());
        customerEmployeeRelation.setDescription(followUserBO.getDescription());
        customerEmployeeRelation.setState(followUserBO.getState());
        customerEmployeeRelation.setBizEventAddTime(followUserCreateTime);
        // 默认有效 员工未授权或不在坐席则无效落数据
        List<Integer> statusList = Lists.newArrayList(EmployeeStatusEnum.ACTIVATED.getCode(), EmployeeStatusEnum.DISABLED.getCode(),
                EmployeeStatusEnum.INACTIVE.getCode(), EmployeeStatusEnum.QUITED.getCode());
        CustomerStatusEnum customerStatus = CustomerStatusEnum.DEFAULT;
        if (EmployeeActiveStatusEnum.INACTIVE.getCode() == employee.getActiveStatus().getCode()
                || !statusList.contains(employee.getStatus().getCode())) {
            customerStatus =  CustomerStatusEnum.UNAUTHORIZED;
        }
        customerEmployeeRelation.setStatus(customerStatus);


        return customerEmployeeRelation;
    }

    public static Set<String> toCustomerIds(List<ExternalUserListBO> externalUserListBOS) {
        if (CollectionUtils.isEmpty(externalUserListBOS)) {
            return new HashSet<>();
        }

        Set<String> customerIds = new HashSet<>();
        for (ExternalUserListBO externalUserListBO : externalUserListBOS) {
            customerIds.add(externalUserListBO.getCustomerId());
        }

        return customerIds;
    }

    public static Map<String, Long> toExternalUserIdAndWidMap(List<ExternalUserBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>();
        }

        Map<String, Long> map = new HashMap<>();
        for (ExternalUserBO externalUserBO : list) {
            map.put(externalUserBO.getExternalUserId(), externalUserBO.getWid());
        }

        return map;
    }

    public static List<String> toExternalUserIdList(List<ExternalUserBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        List<String> externalUserIds = new ArrayList<>();
        for (ExternalUserBO externalUserBO : list) {
            externalUserIds.add(externalUserBO.getExternalUserId());
        }

        return externalUserIds;
    }

    public static List<String> toWidList(List<ExternalUserListBO> externalUserListBOS) {
        if (CollectionUtils.isEmpty(externalUserListBOS)) {
            return new ArrayList<>();
        }

        List<String> widList = new ArrayList<>();
        for (ExternalUserListBO externalUserListBO : externalUserListBOS) {
            widList.add(externalUserListBO.getExternalUserWid() + "");
        }


        return widList;
    }

    public static List<String> toExternalUserIdListFromMapping(List<PassPortAccountMappingRes> accountMappingList) {
        if (CollectionUtils.isEmpty(accountMappingList)) {
            return new ArrayList<>();
        }


        Set<String> externalUserIdSet = new HashSet<>();
        for (PassPortAccountMappingRes mappingRes : accountMappingList) {
            externalUserIdSet.add(mappingRes.getExternalUserId());
        }

        if (CollectionUtils.isEmpty(externalUserIdSet)) {
            return new ArrayList<>();
        }

        return Lists.newArrayList(externalUserIdSet);
    }
}
